import { reactive, ref } from 'vue'
import { FormInstance } from 'element-plus'
export default function () {
  let formInstance = ref<FormInstance>()
  const validateTmName = (rule: any, value: string, callback: any) => {
    if (value.length < 2 || value.length > 8) {
      callback(new Error('长度必须在2到7位之间'))
    }
    callback()
  }

  const validateLogoUrl = (rule: any, value: string, callback: any) => {
    if (value) {
      callback()
    } else {
      callback(new Error('请上传图片'))
    }
  }

  const clearFormValidate = () => {
    formInstance.value?.clearValidate('logoUrl')
    formInstance.value?.clearValidate('tmName')
  }
  const rules = reactive<any>({
    tmName: [{ require: true, trigger: 'change', validator: validateTmName }],
    logoUrl: [{ validator: validateLogoUrl }],
  })

  return {
    rules,
    formInstance,
    clearFormValidate,
  }
}
